package com.kehutong.payfees.util;

import com.kehutong.common.DoveClient;
import com.kehutong.common.util.Token;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.util.Objects;

public class FlowClient {

    @Inject
    private DoveClient doveClient;

    public JSONArray getFlowOffers(String companyNo, JSONObject json) throws Exception {
        JSONObject result = doveClient.post("/flow/service/offer/list", http->{
            JSONObject body = new JSONObject();

            String communityNo = json.getString("communityNo");
            if(Objects.nonNull(communityNo)){
                body.put("communityNo", communityNo);
            }

            String roleNo = json.getString("roleNo");
            if(Objects.nonNull(roleNo)){
                body.put("roleNo", roleNo);
            }

            roleNo = json.getString("roleId");
            if(Objects.nonNull(roleNo)){
                body.put("roleNo", roleNo);
            }

            String employeeNo = json.getString("employeeNo");
            if(Objects.nonNull(employeeNo)){
                body.put("employeeNo", employeeNo);
            }
            body.put("pageNo", 1);
            body.put("pageSize", 1000000);

            http.addHeader("companyNo", companyNo);
            http.setBody(body.toString());
        });

        JSONArray data = result.getJSONArray("data");

        if (Objects.isNull(data)) {
            return null;
        }

        return data;
    }
}
